package org.niugang.mq;

import org.niugang.bean.LogQueryBean;
import org.niugang.service.LogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;

/**
 * 消息接接受者测试 Copyright: Copyright (c) 2018 863263957@qq.com 监听接收 各个服务发从的日志
 * 
 * @ClassName: MyReceiver.java
 * @version: v1.0.0
 * @author: niugang
 * @date: 2018年9月18日 下午5:37:42
 */
@Component
public class ReceiveMessage {

	@Autowired
	private  LogService logService;
	
	private static final Logger logger = LoggerFactory.getLogger(ReceiveMessage.class);

	/**
	 * 接收队列
	 */
	@RabbitListener(bindings = @QueueBinding(value = @Queue(value = "microservice-user-log", durable = "true"), exchange = @Exchange(value = "microservice-base-log-exchange", ignoreDeclarationExceptions = "true"), key = "microservice-base-log-route-key"))
	public void process(Message message) {
		byte[] body = message.getBody();
		String msgBody = new String(body);
		logger.info("microservice-user-logs:{}", msgBody);
		LogQueryBean parseObject = JSON.parseObject(msgBody, LogQueryBean.class);
		logService.insert(parseObject);

	}

}
